package hz.mobileproject.pda.ui.activity.work;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.LogUtils;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.EventTypeBean;
import hz.mobileproject.pda.entity.MessageEvent;
import hz.mobileproject.pda.push.mqtt.MqttClientMgr;
import hz.mobileproject.pda.ui.adapter.GridManagerFragmentAdapter;
import hz.mobileproject.pda.ui.frgment.BeingProcessFragment;
import hz.mobileproject.pda.ui.frgment.NoneProcessFragment;
import hz.mobileproject.pda.utils.SnackbarUtils;

/**
 * 网格化管理界面
 */
public class GridManagerActivity extends AppCompatActivity {

    private String[] titles = {"未处理","处理中"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private SegmentTabLayout tb1;
    private ViewPager vp;

    private Button bt_close;

    private boolean isOnce = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_manager);

        initView();
        initData();

//        IMClientManager.getInstance(this).getTransDataListener().setPush_Now(this);
        EventBus.getDefault().register(this);

        try{
            IntentFilter filter = new IntentFilter();
            filter.addAction(MqttClientMgr.ACTION_CONNECTION_LOST);
            filter.addAction(MqttClientMgr.ACTION_MESSAGE_ARRIVED);
            registerReceiver(mPushReceiver,filter);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    private void initView() {
        tb1 = (SegmentTabLayout) findViewById(R.id.tl_1);
        vp = (ViewPager) findViewById(R.id.vp);
        bt_close = (Button) findViewById(R.id.bt_close);
        bt_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vp.setCurrentItem(1);
            }
        });
    }

    private void initData() {
        mFragments.add(NoneProcessFragment.getInstance());
        mFragments.add(BeingProcessFragment.getInstance());
        vp.setAdapter(new GridManagerFragmentAdapter(getSupportFragmentManager(),mFragments));
        tb1.setTabData(titles);
//        tb1.showMsg(1,5);
//        tb1.showDot(1);  显示红点
        tb1.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tb1.setCurrentTab(position);
                mFragments.get(position).onResume();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp.setCurrentItem(0);
    }




    private PushReceiver mPushReceiver = new PushReceiver();

    class PushReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(MqttClientMgr.ACTION_CONNECTION_LOST)){
            }else if (intent.getAction().equals(MqttClientMgr.ACTION_MESSAGE_ARRIVED)){
                try {
//                    String topWord = "topic/" + SPUtils.getInstance("app_login").getString("department_id") + "/alarm";
                    String msgContent = intent.getStringExtra("content");
                    JSONObject msg = new JSONObject(msgContent);
                    String type = msg.getString("push_type");
                    String push_data = msg.getString("push_data");
                    if("2001".equals(type)) {
                        LogUtils.e("网格化推送" + msg);
                        JSONObject jsonObject= new JSONObject(push_data);
                        String state=jsonObject.getString("state");

                        if("1".equals(state)) {
                            //未处理
                            vp.setCurrentItem(0);
                        }else if("2".equals(state)) {
                            //处理中
                            vp.setCurrentItem(1);
                        }else if("3".equals(state)) {
                            //处理完成

                        }

                        EventBus.getDefault().post(new MessageEvent("网格推送"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

        if (null != mPushReceiver){
            unregisterReceiver(mPushReceiver);
        }
    }

    //超时
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventTypeBean event){
        //收到Activity的消息
        if(isOnce) {
            if ("超时".equals(event.getMsg())) {
                SnackbarUtils.Long(vp, "未处理中有超时事件").setAction("跳转", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        vp.setCurrentItem(1);
                    }
                }).show();
            }
            isOnce = !isOnce;
        }
    }


}
